package com.github.binarylei.curator.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock
 * @author: leigang
 * @version: 2018-04-06
 */
public class Lock1 {
    
    private static ReentrantLock reentrantLock = new ReentrantLock();
    private static int count = 10;
    
    public static void genarNo() {
        try {
            reentrantLock.lock();
            count--;
            System.out.println(count);
        } finally {
            reentrantLock.unlock();
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                        genarNo();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        Thread.sleep(1000);
        countDownLatch.countDown();
    } 
}
